﻿<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: User.php 5336 2010-09-07 08:15:47Z huuphuoc $
 * @since		2.0.5
 */

class Bizprofile_Models_Dao_Pdo_Mysql_Partner extends Tomato_Model_Dao
	implements Bizprofile_Models_Interface_Partner
{
	public function convert($entity)
	{
		return new Bizprofile_Models_Partner($entity); 
	}
	
	public function add($partner) 
	{
		$this->_conn->insert($this->_prefix . 'enterprise_list', 
							array(
								'enterprise_id'	 => $partner->enterprise_id,
								'relationid' 	 => $partner->relationid,
								'partner_type' 	 => $partner->partner_type,
								'partner_status' => $partner->partner_status
							));
		return $this->_conn->lastInsertId($this->_prefix . 'enterprise_list');
	}
	
	public function add2($partner) 
	{
		$this->_conn->insert($this->_prefix . 'enterprise_list', 
							array(
								'enterprise_id'	 => $partner->enterprise_id,
								'relationid' 	 => $partner->relationid,
								'partner_type' 	 => $partner->partner_type,
								'partner_status' => $partner->partner_status
							));
		$this->_conn->insert($this->_prefix . 'enterprise_list', 
							array(
								'enterprise_id'	 => $partner->relationid,
								'relationid' 	 => $partner->enterprise_id,
								'partner_type' 	 => $partner->partner_type,
								'partner_status' => $partner->partner_status
							));
		return $this->_conn->lastInsertId($this->_prefix . 'enterprise_list');
	}
	// hàm này dùng để getPartner nhưng không có partner_type="buddy"
	// làm thế này mới get được mấy cái ingnore
	public function getPartner_($enterp_id, $rela_id){
		$row = $this->_conn
					->select()
					->from(array('u' => $this->_prefix . 'enterprise_list'))
					->where('u.enterprise_id = ?', $enterp_id)
					->where('u.relationid = ?', $rela_id)
					->limit(1)
					->query()
					->fetch();
    	return (null == $row) ? null : new Bizprofile_Models_Partner($row);	
	}
	public function getPartner($enterp_id, $rela_id){
		$row = $this->_conn
					->select()
					->from(array('u' => $this->_prefix . 'enterprise_list'))
					->where('u.enterprise_id = ?', $enterp_id)
					->where('u.relationid = ?', $rela_id)
					->where('u.partner_type = ?', "buddy")
					->limit(1)
					->query()
					->fetch();
    	return (null == $row) ? null : new Bizprofile_Models_Partner($row);
	}
	
	public function acceptPartner($enterp_id, $rela_id){
            $this->_conn->update($this->_prefix . 'enterprise_list',
                array(
                    'partner_status'	=> "yes"
                ),
                array(
                    'enterprise_id = ?' => $enterp_id,
                    'relationid' => $rela_id
                ));
			
            $this->_conn->update($this->_prefix . 'enterprise_list',
                array(
                    'partner_status'	=> "yes"
                ),
                array(
                    'enterprise_id = ?' => $rela_id,
                    'relationid' => $enterp_id
                ));
	}

        /*
         * Deny partner add by Cong Ngo
         */
        public function denyPartner($enterp_id, $rela_id){
			// mình từ chối người khác
            $this->_conn->update($this->_prefix . 'enterprise_list',
                array(
                    'partner_status'	=> "denied",
                    'partner_type'      => "ignore",
					'notify'			=> "unview" 
                ),
                array(
                    'enterprise_id = ?' => $enterp_id,
                    'relationid' 		=> $rela_id
                ));
			// người khác bị mình từ chối
            $this->_conn->update($this->_prefix . 'enterprise_list',
                array(
                    'partner_status'	=> "pending",
                    'partner_type'		=> "ignore",
					'notify'			=> "unview" 
                ),
                array(
                    'enterprise_id = ?' => $rela_id,
                    'relationid' => $enterp_id
                ));
		}
        /*
         * End deny method
         */

	public function checkPartner($enterp_id, $rela_id){
		$tmp = $this->getPartner($enterp_id, $rela_id);
		if ($tmp == null) return false;
		return true;
	}
	
	public function removePartner($enterp_id, $rela_id){
		$sql = "Delete From ".$this->_prefix."enterprise_list where enterprise_id=$enterp_id and relationid=$rela_id";
		$this->_conn->query($sql);
		$sql = "Delete From ".$this->_prefix."enterprise_list where enterprise_id=$rela_id and relationid=$enterp_id";
		$this->_conn->query($sql);
	}
	
	public function listPartner($enterp_id){
		$prefix = $this->_prefix;
		$sql = "Select *
				From ".$prefix."enterprise 
				Where id IN (Select relationid From ".$prefix."enterprise_list Where enterprise_id=".$enterp_id." and partner_status='yes' and partner_type='buddy') 
				   or id IN (Select enterprise_id From ".$prefix."enterprise_list Where relationid=".$enterp_id." and partner_status='yes' and partner_type='buddy') ";
		//echo $sql;
	   	$rs  = $this->_conn->query($sql)->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function getListByEnteprise($enterp_id, $offset, $count){
		$prefix = $this->_prefix;
		$sql = "Select *
				From ".$prefix."enterprise 
				Where id IN (Select relationid From ".$prefix."enterprise_list Where enterprise_id=".$enterp_id." and partner_status='yes' and partner_type='buddy') 
				   or id IN (Select enterprise_id From ".$prefix."enterprise_list Where relationid=".$enterp_id." and partner_status='yes' and partner_type='buddy') 
				LIMIT ".$offset.",".$count." ";		
		$rs  = $this->_conn->query($sql)->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function getCountListByEnteprise($enterp_id){
		$prefix = $this->_prefix;
		$sql = "Select *
				From ".$prefix."enterprise 
				Where id IN (Select relationid From ".$prefix."enterprise_list Where enterprise_id=".$enterp_id." and partner_status='yes' and partner_type='buddy') 
				   or id IN (Select enterprise_id From ".$prefix."enterprise_list Where relationid=".$enterp_id." and partner_status='yes' and partner_type='buddy') ";
		//echo $sql;
	   	$rs  = $this->_conn->query($sql)->fetchAll();
		$tmp = new Tomato_Model_RecordSet($rs, $this);
		if (isset($tmp)) return count($tmp);
		return 0;
	}
	
	public function getNotify($enterprise_id){
		$row = $this->_conn
					->select()
					->from(array('u' => $this->_prefix . 'enterprise_list'))
					->where('u.relationid = ?', $enterprise_id)
					->where('u.partner_type = ?', "buddy")
					->where('u.notify = ?', "unview")
					->query()
					->fetch();
		return new Tomato_Model_RecordSet($row, $this);
	}
	
	public function getNotifyPartner($enterprise_id){
		$prefix = $this->_prefix;
		$sql = "Select e.*,el.* 
				From ".$prefix."enterprise e INNER JOIN ".$prefix."enterprise_list el ON e.Id=el.enterprise_id 
				Where partner_type='buddy' and partner_status='no' and notify='unview' and relationid=".$enterprise_id;
		$rs  = $this->_conn->query($sql)->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function getCountNotify($enterprise_id){
		$prefix = $this->_prefix;
		$sql = "Select * 
				From ".$prefix."enterprise_list 
				Where partner_type='buddy' and notify='unview' and partner_status = 'no' and relationid=".$enterprise_id;
		
		$rs  = $this->_conn->query($sql)->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function updateNotify($enterprise_id){
		$prefix = $this->_prefix;
		$sql = "Update ".$prefix."enterprise_list
				Set notify='view' 
				Where partner_type='buddy' and notify='unview' and partner_status = 'no' and enterprise_id=".$enterprise_id;
        return $this->_conn->query($sql);		
	}
	
	// UPDATE MOI RECOMMENT DOI TAC GUI CHO MINH
	// KHI DO MINH DONG VAI TRO LA RELATION_ID
	public function updateNotifyUnreadRecomment($enterprise_id){
		$prefix = $this->_prefix;
		$sql = "Update ".$prefix."enterprise_list
				Set notify='view' 
				Where partner_type='buddy' and notify='unview' and partner_status = 'yes' and relationid=".$enterprise_id;
        return $this->_conn->query($sql);		
	}	
	
	public function count($exp)
	{
		$select = $this->_conn
						->select()
						->from(array('m' => $this->_prefix . 'enterprise_list'), array('num_enterprises' => 'COUNT(*)'))
						->joinLeft(array('e' => $this->_prefix . 'enterprise'), 'e.id = m.enterprise_id', array(
							 'company_name_vn' => 'company_name_vn', 
							 'company_name_en' => 'company_name_en',
					   		 'contact_email' => 'contact_email',  	
					   		 'contact_tel' => 'contact_tel',  	
					   		 'contact_fax' => 'contact_fax',  	
					   		 'contact_address' => 'contact_address',  	
							 'frontend_active' => 'frontend_active',
							 'deleted' => 'deleted'
						));
		if ($exp) {
			if (isset($exp['enterprise_id'])) {
				$select->where('m.enterprise_id = ?', $exp['enterprise_id']);
			}
			if (isset($exp['relationid'])) {
				$select->where('m.relationid = ?', $exp['relationid']);
			}
			if (isset($exp['partner_type'])) {
				$select->where('m.partner_type = ?', $exp['partner_type']);
			}
			if (isset($exp['partner_status'])) {
				$select->where('m.partner_status = ?', $exp['partner_status']);
			}			
			if (isset($exp['notify'])) {
				$select->where('m.notify = ?', $exp['notify']);
			}	
			if (isset($exp['recomment'])) {
				$select->where('m.recomment is not null');
			}			
			if (isset($exp['deleted'])) {
				$select->where('e.deleted != 1');
			}						
		}
//		echo $select;
		return $select->limit(1)->query()->fetch()->num_enterprises;
	}
	
	public function find($offset = null, $count = null, $exp = null)
	{
		$select = $this->_conn
						->select()
						->from(array('m' => $this->_prefix . 'enterprise_list'))
						->joinLeft(array('e' => $this->_prefix . 'enterprise'), 'e.id = m.enterprise_id', array(
							 'company_name_vn' => 'company_name_vn', 
							 'company_name_en' => 'company_name_en',
					   		 'contact_email' => 'contact_email',  	
					   		 'contact_tel' => 'contact_tel',  	
					   		 'contact_fax' => 'contact_fax',  	
					   		 'contact_address' => 'contact_address',  	
							 'frontend_active' => 'frontend_active',
							 'deleted' => 'deleted'
						));
		if ($exp) {
			if (isset($exp['enterprise_id'])) {
				$select->where('m.enterprise_id = ?', $exp['enterprise_id']);
			}
			if (isset($exp['relationid'])) {
				$select->where('m.relationid = ?', $exp['relationid']);
			}
			if (isset($exp['partner_type'])) {
				$select->where('m.partner_type = ?', $exp['partner_type']);
			}
			if (isset($exp['partner_status'])) {
				$select->where('m.partner_status = ?', $exp['partner_status']);
			}			
			if (isset($exp['notify'])) {
				$select->where('m.notify = ?', $exp['notify']);
			}
			if (isset($exp['recomment'])) {
				$select->where('m.recomment is not null');
			}
			if (isset($exp['deleted'])) {
				$select->where('e.deleted != 1');				
			}			
			if (isset($exp['deleted_relation'])) {
				$select->where('m.relationid IN (SELECT f.id FROM `'.$this->_prefix . 'enterprise'.'` as f WHERE f.deleted !=1 )');
			}
					
		}
		if (is_int($offset) && is_int($count)) {
			$select->limit($count, $offset);
		}		
		//echo($select);
		$rs = $select->query()->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	function updaterecomment($recomment){
		return $this->_conn->update($this->_prefix .'enterprise_list',
                            array(
								'recomment' => $recomment['recomment'],
							),
							array(
								'enterprise_id = ?'    => $recomment['relationid'],
								'relationid    = ?'    => $recomment['enterprise_id'],
							));
	}
}
